package system.notify;

import java.util.ArrayList;
import java.util.HashMap;

import com.hong.alert.dao.ReferenceDataRepossitory;

import system.main.SystemDataStructure;
import system.queues.AlertQueue;
import system.queues.WarningQueue;
import system.wrap_filter.Standard;

public class Notifier extends Thread {
	SystemDataStructure ds;
	AlertQueue alertQueue;
	WarningQueue warningQueue;
	Emailer emailer;
	private ReferenceDataRepossitory referenceRepository;
	ArrayList<Admin> admins;
	HashMap<String, String> hashMap;
	boolean run;

	public Notifier(SystemDataStructure ds) {
		this.ds = ds;
		this.warningQueue = ds.getWarningQueue();
		this.alertQueue = ds.getAlertQueue();
		this.referenceRepository = ds.getReferenceDataRepossitory();
		this.admins = (ArrayList<Admin>) referenceRepository.admins();
		this.hashMap = new HashMap<String, String>();
		for (Admin admin : admins) {
			hashMap.put(admin.getType(), admin.getAddress());
		}
		run = true;
	}

	@Override
	public void run() {

		while (run) {
			if (alertQueue.getSize() > 0) {
				Standard standard = alertQueue.poll();
				if (standard != null) {
					this.emailer = new Emailer(standard, hashMap.get(standard
							.getSource()));
				}
			} else if (warningQueue.getSize() > 0) {
				Standard standard = warningQueue.poll();
				this.emailer = new Emailer(standard, hashMap.get(standard
						.getSource()));
			}
		}

	}
}
